﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using CaiBaby.Common;
using CaiBaby.Entity;

namespace CaiBaby.DAL
{
    /// <summary>
    /// 管理员权限类别数据访问层
    /// </summary>
    public class MangerPowerClass
    {
        /// <summary>
        /// 增加一个权限类别
        /// </summary>
        /// <param name="mangerPowerInfo">权限类别实体类</param>
        /// <returns>返回权限类别ID</returns>
        public int Add(MangerPowerClassInfo mangerPowerClassInfo)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("INSERT INTO ma_MangerPowerClass(");
            strSql.Append("Cname)");
            strSql.Append(" VALUES (");
            strSql.Append("@Cname)");
            strSql.Append(";SELECT @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@Cname", SqlDbType.Int,4)};
            parameters[0].Value = mangerPowerClassInfo.Cname;

            object obj = SQLHelper.GetSingle(ConfigHelper.CaiBabyDBConnStr, CommandType.Text, strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 修改一个权限类别
        /// </summary>
        /// <param name="mangerPowerClassInfo">权限类别实体类</param>
        /// <returns>返回是否成功</returns>
        public bool Update(MangerPowerClassInfo mangerPowerClassInfo)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("UPDATE ma_MangerPowerClass SET ");
            strSql.Append("Cname=@Cname) ");
            strSql.Append("WHERE CId=@CId ");
            SqlParameter[] parameters = {
					new SqlParameter("@CId", SqlDbType.Int,4),
					new SqlParameter("@Cname", SqlDbType.VarChar,32)};
            parameters[0].Value = mangerPowerClassInfo.CId;
            parameters[1].Value = mangerPowerClassInfo.Cname;

            int rows = SQLHelper.ExecuteSql(ConfigHelper.CaiBabyDBConnStr, CommandType.Text, strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一个权限类别
        /// </summary>
        /// <param name="cId">权限类别ID</param>
        /// <returns>返回是否成功</returns>
        public bool Delete(int cId)
        {
            string strSql1 = "DELETE FROM ma_MangerPowerClass WHERE CId=@CId";
            string strSql2 = "UPDATE ma_MangerPower SET CId = 0 WHERE CId=@CId";
            SqlParameter[] parameters = {
					new SqlParameter("@CId", SqlDbType.Int,4)};
            parameters[0].Value = cId;
            using (SqlTransaction tran = SQLHelper.BeginTransaction(ConfigHelper.CaiBabyDBConnStr))
            {
                try
                {
                    SQLHelper.ExecuteSql(tran, CommandType.Text, strSql1, parameters);
                    SQLHelper.ExecuteSql(tran, CommandType.Text, strSql2, parameters);
                    tran.Commit();
                }
                catch (Exception ex)
                {
                    tran.Rollback();
                    return false;
                }
            }
            return true;
        }
    }
}
